<template>
	<view >
		<!-- :name="address"：给组件传入参数 ，
		v-on:parentCourseSearch="parentCourseSearch" 是给子组件绑定方法，子组件可以通过this.$emit("parentCourseSearch"掉这个方法 -->
		<ComponentHeader :name="address" v-on:parentCourseSearch="parentCourseSearch"></ComponentHeader>
		<ComponentSwiper ></ComponentSwiper>
		<view class="type-view">
			
				<view @click="clicktype" :data-tid="t.id" class="type-item-view" v-for="t in types">
						<view>
								<image :src="t.icon"></image>
						</view>
						<view>
							<text>{{t.title}}</text>
						</view>
				</view >
		
			
			<view class="type-item-view" >
				<navigator style="display: inline"  url="../coursetype/coursetype" open-type="navigate">
					<view>
							<image src="../../static/type4.png"></image>
					</view>
					<view>
						<text>全部分类</text>
					</view>
				</navigator>
			</view >
		</view>
		<view class="recommend-view">
			<text> / 热门推荐 / </text>
		</view>
		<view class="course-list-view">
			<view class="course-item-view" v-for="c in list">
				<navigator class="course-item-nav" :url="'../detail/detail?id='+c.id" open-type="navigate">
					<view class="course-item-img-view">
						<image :src="c.pic"></image>
					</view>
				</navigator>
					<view class="course-item-title-view" >
						<view class="course-item-name-view">{{c.name}}</view>
						<view class="course-item-bar-view">
							<text>{{c.gradeName}}  | {{c.timeLong}} 分钟</text>			
						</view>
					</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address:"",
				postCount: 0,
				pageSize: 12,
				pageNum: 0,
				pages:1,
				list: [],
				types:[
					{
						icon:"../../static/icon1.png",
						title:"热门",
						id:1
					},
					{
						icon:"../../static/icon2.png",
						title:"减脂",
						id:2
					},
					{
						icon:"../../static/icon1.png",
						title:"增肌",
						id:3
					},
					{
						icon:"../../static/icon2.png",
						title:"塑形",
						id:4
					},
					{
						icon:"../../static/icon1.png",
						title:"舞蹈",
						id:5
					}
				]

			}
		},
		created() {
			var _this = this;
			uni.getLocation({
				type: 'wgs84',
				success: function (res) {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);
					//转换地理位置
					uni.request({
						url:"https://restapi.amap.com/v3/geocode/regeo",
						data:{
							key:"faeeb02bc17355549996e51933d2c045", //高德地图的key
							location: res.longitude+","+res.latitude
						},
						success(res) {
							var add = res.data.regeocode.addressComponent.city;
							_this.address = add; //获取地址，赋值
							_this.initLoad();
						},
						fail() {
							uni.showToast({
								title:"定位失败",
								success() {
									_this.initLoad();
								}
							})
						},complete() {
							
						}
					})
				}
			});
		},
		
		//页面滑动到底部
		onReachBottom() {
			return;//首页不分页
			//分页到底了
			if(this.pageNum == this.pages){
				uni.showToast({
					title:"到底了",
					icon:"loading",
					duration:500
				})
				return;
			}
			//分页+1
			this.pageNum = this.pageNum+1
			//加载数据
			this.getList();
			
			
		},
		methods: {
			clicktype(e){
				let tid = e.currentTarget.dataset.tid;
				uni.reLaunch({
					url:"/pages/course/course?typeid="+tid
				})
			},
			initLoad() {
				this.pageNum = 1
				//加载数据
				this.getList();
			},
			//父组件，搜索功能
			parentCourseSearch(keyword){
				this.keyword = keyword;
				this.pageNum = 0;//查询显示，第一页
				this.getList();
			},
			getList() {
				//页面滚动到底部触发
				uni.showLoading({title:"加载中...",})
				
				var _this = this;
				uni.request({
					url: 'http://127.0.0.1:1000/course/fitnessCourse/page',
					data:{
						"page":_this.pageNum,
						"rows":_this.pageSize,
						"keyword":_this.keyword,
						"address":_this.address
					},
					method: 'POST',
					success: res => {
						//拼接数据
						
						_this.list = _this.list.concat(res.data.data.rows);
						//总条数
						_this.postCount = res.data.data.total;
						//总页数
						_this.pages = _this.postCount % _this.pageSize == 0?_this.postCount / _this.pageSize:_this.postCount / _this.pageSize+1;
					},
					fail: function(e) {
						uni.showToast({
							title:"唉哟，程序错误了["+JSON.stringify(e)+"]",
							icon:"loading"
						})
					},
					complete() {
						//关闭进度条
						setTimeout(function () {
						    uni.hideLoading();
						}, 500);
					}
				});
			}
		}
		
	}
</script>
<style lang="scss" scoped>
	
	.type-view{
		width: 96%;
		margin: 2%;
		height: auto;
		text-align: center;
	}
	.type-item-view{
		text-align: center;
		width: 32%;
		display: inline-block;
		margin-top: 20rpx;
		font-size: 32rpx;
	}
	.type-view image{
		width: 80rpx;
		height: 80rpx;
		border-radius: 100%;
		border: 2rpx solid #e0e0e0;
	}
	.course-item-nav{
		height: auto;
	}
	.swiper-item-view image{
		width: 100%;
	}
	.recommend-view{
		align-items: center;
		text-align: center;
		margin-top: 40rpx;
		border-top: 1rpx solid #D3D3D3;
		border-bottom: 1rpx solid #D3D3D3;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		color: #636363;
	}
	
	.course-list-view{
		padding: 10px;
		clear: both;
		padding-bottom: 20rpx;
		background-image: linear-gradient(#ffffff, #ececec,#f6f6f6);
		vertical-align: top;
	}
	.course-item-view{
		width: 46%;
		height: auto;
		display: inline-block;
		margin-top: 20rpx;
		margin-left: 2%;
		background-color: #ffffff;
		border-radius: 10rpx;
		position: relative;
	}
	
	.course-item-img-view{
		height: 100%;
		clear: both;
	}
	.course-item-img-view image{
		width: 100%;
		height: 180rpx;
		border-radius: 10rpx;
	}
	.course-item-title-view{
		height: auto;
		margin-left: 15rpx;
		margin-right: 15rpx;
	}
	.course-item-name-view{
		height: 70rpx;
		color: #333333;
		font-size:28rpx;
		overflow: hidden;
	}
	.course-item-bar-view{
		height: 20%;
		margin-bottom: 10rpx;
		color: #808080;
	}

	.course-item-bar-view text{
		color: #cece00;
		font-size:24rpx;
	}

</style>
